package lu.med.praxiswahl.wahlumed.client.lieferanten;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

/**
 * 
 * @author Lars Holger Roth
 */
public class LieferantenCellRenderer implements TableCellRenderer
{
    private Color black = new Color(0, 0, 190);
    private Color white = new Color(255, 255, 255);
    private Color red = new Color(255, 0, 0);
    private Color grey = new Color(200, 200, 200);
    private Color blue = new Color(0, 0, 250);
    private Color yellow = new Color(200, 200, 0);
    private Font bold = new Font("Monospaced", Font.BOLD, 12);
    private LieferantenTableModel model;

    public LieferantenCellRenderer(LieferantenTableModel model)
    {
	this.model = model;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
	    boolean isSelected, boolean hasFocus, int row, int column)
    {
	// Wenn die Menge des Artikels knapp ist, so wird die entsprechende
	// Zeile Wei� auf Rot dargestellt, ansonsten Schwarz auf Wei�
	JLabel label = new JLabel();

	label.setOpaque(true);
	label.setHorizontalAlignment(SwingConstants.CENTER);
	Border b = BorderFactory.createEmptyBorder(1, 1, 1, 1);
	label.setBorder(b);
	label.setFont(table.getFont());
	// �berpr�fen ob der Artikel knapp ist
	if (model.isAlert(row))
	{
	    label.setForeground(white);
	    label.setBackground(red);
	    label.setFont(bold);
	}
	else if (model.isNotPaid(row))
	{
	    label.setForeground(black);
	    label.setBackground((yellow));
	    label.setFont(bold);
	}
	else
	{
	    if (row % 2 == 0)
	    {
		label.setForeground(table.getForeground());
		label.setBackground(table.getBackground());
	    }
	    else
	    {
		// Jede zweite Zeile bekommt grauen Hintergrund
		label.setForeground(table.getForeground());
		label.setBackground(grey);
	    }
	}
	label.setText(model.getValue(row, column));
	if (hasFocus)
	{
	    label.setBackground(black);
	    label.setForeground(white);
	}
	else
	{
	    // Angezeigte Spalte in Modellspalte umwandeln
	    column = table.convertColumnIndexToModel(column);
	}

	if (isSelected)
	{
	    label.setBackground(blue);
	    label.setForeground(white);
	    label.setFont(bold);
	}

	return label;
    }

}
